<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试页面</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            border: 1px solid #ddd;
            padding: 20px;
            margin-bottom: 20px;
            border-radius: 5px;
        }
        button {
            background: #4CAF50;
            color: white;
            border: none;
            padding: 10px 15px;
            cursor: pointer;
            border-radius: 3px;
        }
        button:hover {
            background: #45a049;
        }
        pre {
            background: #f5f5f5;
            padding: 15px;
            border-radius: 3px;
            overflow-x: auto;
        }
        .error {
            color: red;
        }
        .success {
            color: green;
        }
    </style>
</head>
<body>
    <h1>API测试页面</h1>
    
    <div class="container">
        <h2>测试 /v1/sessions</h2>
        <button id="test-sessions">测试</button>
        <div id="sessions-result"></div>
    </div>
    
    <div class="container">
        <h2>测试 /v1/sessions/{id}/messages</h2>
        <input type="text" id="chat-id" value="test-chat-123" placeholder="输入session_id">
        <button id="test-history">测试</button>
        <div id="history-result"></div>
    </div>
    
    <div class="container">
        <h2>测试 /v1/llm/ollama/models</h2>
        <button id="test-models">测试</button>
        <div id="models-result"></div>
    </div>

    <script>
        const baseURL = 'http://localhost:8001/v1';
        
        // 自动在页面加载时测试所有API
        window.onload = async () => {
            await testAllAPIs();
        };
        
        // 测试sessions API
        async function testSessionsAPI() {
            const resultDiv = document.getElementById('sessions-result');
            resultDiv.innerHTML = '加载中...';
            
            try {
                const response = await axios.get(`${baseURL}/sessions`);
                resultDiv.innerHTML = `<div class="success">成功!</div><pre>${JSON.stringify(response.data, null, 2)}</pre>`;
                return true;
            } catch (error) {
                resultDiv.innerHTML = `<div class="error">失败: ${error.message}</div><pre>${JSON.stringify(error.response?.data || error, null, 2)}</pre>`;
                return false;
            }
        }
        
        // 测试会话消息 API
        async function testHistoryAPI() {
            const sessionId = document.getElementById('chat-id').value;
            const resultDiv = document.getElementById('history-result');
            resultDiv.innerHTML = '加载中...';
            
            try {
                const response = await axios.get(`${baseURL}/sessions/${sessionId}/messages`);
                resultDiv.innerHTML = `<div class="success">成功!</div><pre>${JSON.stringify(response.data, null, 2)}</pre>`;
                return true;
            } catch (error) {
                resultDiv.innerHTML = `<div class="error">失败: ${error.message}</div><pre>${JSON.stringify(error.response?.data || error, null, 2)}</pre>`;
                return false;
            }
        }
        
        // 测试ollama models API
        async function testModelsAPI() {
            const resultDiv = document.getElementById('models-result');
            resultDiv.innerHTML = '加载中...';
            
            try {
                const response = await axios.get(`${baseURL}/llm/ollama/models`);
                resultDiv.innerHTML = `<div class="success">成功!</div><pre>${JSON.stringify(response.data, null, 2)}</pre>`;
                return true;
            } catch (error) {
                resultDiv.innerHTML = `<div class="error">失败: ${error.message}</div><pre>${JSON.stringify(error.response?.data || error, null, 2)}</pre>`;
                return false;
            }
        }
        
        // 测试所有API
        async function testAllAPIs() {
            console.log('开始测试所有API...');
            const sessionsSuccess = await testSessionsAPI();
            await new Promise(resolve => setTimeout(resolve, 1000));
            const historySuccess = await testHistoryAPI();
            await new Promise(resolve => setTimeout(resolve, 1000));
            const modelsSuccess = await testModelsAPI();
            
            const allSuccess = sessionsSuccess && historySuccess && modelsSuccess;
            console.log(`所有API测试${allSuccess ? '成功' : '失败'}`);
        }
        
        // 仍然保留手动测试按钮功能
        document.getElementById('test-sessions').addEventListener('click', testSessionsAPI);
        document.getElementById('test-history').addEventListener('click', testHistoryAPI);
        document.getElementById('test-models').addEventListener('click', testModelsAPI);
    </script>
</body>
</html>
